package com.ruoyi.edh.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.edh.domain.EdhBill;
import com.ruoyi.edh.domain.EdhCardCase;
import com.ruoyi.edh.domain.EdhProfit;
import com.ruoyi.edh.domain.vo.EdhWithdrawVo;
import com.ruoyi.edh.mapper.EdhBillMapper;
import com.ruoyi.edh.mapper.EdhCardCaseMapper;
import com.ruoyi.edh.mapper.EdhProfitMapper;
import com.ruoyi.edh.mapper.EdhWithdrawMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.edh.domain.EdhWithdraw;
import com.ruoyi.edh.service.IEdhWithdrawService;

/**
 * 易订货-提现审核Service业务层处理
 *
 * @author xxx
 * @date 2020-09-21
 */
@Service
public class EdhWithdrawServiceImpl implements IEdhWithdrawService
{
    @Autowired
    private EdhWithdrawMapper edhWithdrawMapper;
    @Autowired
    private EdhProfitMapper edhProfitMapper;
    @Autowired
    private EdhBillMapper billMapper;
    @Autowired
    private EdhCardCaseMapper cardCaseMapper;
    /**
     * 查询易订货-提现审核
     *
     * @param id 易订货-提现审核ID
     * @return 易订货-提现审核
     */
    @Override
    public EdhWithdraw selectEdhWithdrawById(Long id)
    {
        return edhWithdrawMapper.selectEdhWithdrawById(id);
    }

    /**
     * 查询易订货-提现审核列表
     *
     * @param edhWithdraw 易订货-提现审核
     * @return 易订货-提现审核
     */
    @Override
    public List<EdhWithdrawVo> selectEdhWithdrawList(EdhWithdraw edhWithdraw)
    {
        return edhWithdrawMapper.queryEdhWithdrawList(edhWithdraw);
    }

    /**
     * 新增易订货-提现审核
     *
     * @param edhWithdraw 易订货-提现审核
     * @return 结果
     */
    @Override
    public int insertEdhWithdraw(EdhWithdraw edhWithdraw)
    {
        Long userId = SecurityUtils.getUserId();
        EdhProfit edhProfit =  edhProfitMapper.selectProfitByUserId(userId);
        if (edhWithdraw.getWithdrawAmount().compareTo(edhProfit.getNotWithdrawn()) == 1){
            throw new CustomException("提现金额不能大于可提现金额");
        }
        edhProfit.setNotWithdrawn(edhProfit.getNotWithdrawn().subtract(edhWithdraw.getWithdrawAmount()));
        edhProfit.setWithdrawn(edhProfit.getWithdrawn().add(edhWithdraw.getWithdrawAmount()));
        edhProfitMapper.updateEdhProfit(edhProfit);
        edhWithdraw.setCreateTime(DateUtils.getNowDate());

        // 增加账单
        EdhBill bill =  new EdhBill();
        String tpyeof = "";
        switch (edhWithdraw.getType()){
            case 1:tpyeof = "支付宝";break;
            case 2:tpyeof = "微信";break;
            default:tpyeof= "银行卡";
                EdhCardCase edhCardCase = cardCaseMapper.selectEdhCardCaseById(edhWithdraw.getCardId());
                edhWithdraw.setUsername(edhCardCase.getUsername());
                edhWithdraw.setAccount(edhCardCase.getCardNumber());
                break;
        }
        bill.setName(tpyeof+"提现申请");
        bill.setCreateBy(userId);
        bill.setCreateTime(DateUtils.getNowDate());
        bill.setPrice("- " + edhWithdraw.getWithdrawAmount().toString());
        billMapper.insertEdhBill(bill);

        return edhWithdrawMapper.insertEdhWithdraw(edhWithdraw);
    }

    /**
     * 修改易订货-提现审核
     *
     * @param edhWithdraw 易订货-提现审核
     * @return 结果
     */
    @Override
    public int updateEdhWithdraw(EdhWithdraw edhWithdraw)
    {
        if ("1".equals(edhWithdraw.getIsReview())){
            EdhProfit edhProfit=new EdhProfit();
            edhProfit.setCreateBy(edhWithdraw.getUserId().toString());
            List<EdhProfit> edhProfits = edhProfitMapper.selectEdhProfitList(edhProfit);
            //获得收益对象
            EdhProfit edhProfit1 = edhProfits.get(0);
            //获取已提现金额，加上本次提现金额，设置已提现金额
            edhProfit.setWithdrawn(edhProfit1.getWithdrawn().add(edhWithdraw.getWithdrawAmount()));
            edhProfit.setCreateBy(null);
            edhProfit.setId(edhProfit1.getId());
            //修改已提现额度
            edhProfitMapper.updateEdhProfit(edhProfit);
        }
        edhWithdraw.setUpdateTime(DateUtils.getNowDate());
        return edhWithdrawMapper.updateEdhWithdraw(edhWithdraw);
    }

    /**
     * 批量删除易订货-提现审核
     *
     * @param ids 需要删除的易订货-提现审核ID
     * @return 结果
     */
    @Override
    public int deleteEdhWithdrawByIds(Long[] ids)
    {
        return edhWithdrawMapper.deleteEdhWithdrawByIds(ids);
    }

    /**
     * 删除易订货-提现审核信息
     *
     * @param id 易订货-提现审核ID
     * @return 结果
     */
    @Override
    public int deleteEdhWithdrawById(Long id)
    {
        return edhWithdrawMapper.deleteEdhWithdrawById(id);
    }
}
